package com.rykj.ext

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.rykj.R
import java.util.concurrent.TimeUnit

/**
 * Description:
author: wangwu on 2018/8/27 15:11.
 */


fun ImageView.loadCircleImage(url: String) {
    Glide.with(this).load(url).apply(RequestOptions.circleCropTransform().placeholder(R.drawable.default_user).error(R.drawable.default_user)).into(this)
}


fun ImageView.loadCircleImage(url: String, @DrawableRes errorInt: Int) {
    Glide.with(this).load(url).apply(RequestOptions.circleCropTransform().placeholder(errorInt).error(errorInt)).into(this)
}


fun ImageView.loadRoundedCornerImage(url: String, roundingRadius: Int) {
    Glide.with(this).load(url)
        .apply(RequestOptions.bitmapTransform(MultiTransformation<Bitmap>(CenterCrop(), RoundedCorners(roundingRadius))).error(R.drawable.default_user)
        ).into(this)
}

///**
// * radius 圆角 单位px
// */
//fun ImageView.loadImageTopRadius(url: Any, radius: Int) {
//    Glide.with(this).load(url).apply(
//        RequestOptions().placeholder(R.drawable.default_img).error(R.drawable.default_img).transform(
//            RoundedCornersTransformation(radius, 0, RoundedCornersTransformation.CornerType.TOP)
//        )
//    ).into(this)
//}

///**
// * radius 圆角 单位px
// */
//fun ImageView.loadImageLeftRadius(url: Any, radius: Int) {
//    Glide.with(this).load(url).apply(
//        RequestOptions().placeholder(R.drawable.default_img).error(R.drawable.default_img).transform(
//            RoundedCornersTransformation(radius, 0, RoundedCornersTransformation.CornerType.LEFT)
//        )
//    ).into(this)
//}

///**
// * radius 圆角 单位px
// */
//fun ImageView.loadImageRadius(url: Any, radius: Int) {
//    Glide.with(this).load(url).apply(
//        RequestOptions().placeholder(R.drawable.default_img).error(R.drawable.default_img).transform(
//            RoundedCornersTransformation(radius, 0, RoundedCornersTransformation.CornerType.ALL)
//        )
//    ).into(this)
//}

fun ImageView.loadImage(url: Any?) {
    val any = url ?: ""
    Glide.with(this).load(any).apply(RequestOptions().placeholder(R.drawable.default_img).error(R.drawable.default_img)).into(this)
}

fun ImageView.loadVideo(url: Any?) {
    val any = url ?: ""
    Glide.with(this).load(any).apply(RequestOptions().placeholder(R.drawable.default_img).error(R.drawable.default_img).frame(TimeUnit.SECONDS.toMicros(1))
    ).into(this)
}

fun ImageView.loadImageNoDefault(url: Any?) {
    val any = url ?: ""
    Glide.with(this).load(any).into(this)
}

fun ImageView.loadImage(uri: Uri) {
    Glide.with(this).load(uri).apply(RequestOptions().placeholder(R.drawable.default_img).error(R.drawable.default_img)).into(this)
}

fun ImageView.loadAvatar(uri: Any) {
    Glide.with(this).load(uri).apply(RequestOptions().placeholder(R.drawable.default_user).error(R.drawable.default_user)).into(this)
}

fun ImageView.loadCircleImage(any: Any) {
    Glide.with(this).load(any).apply(RequestOptions.circleCropTransform().placeholder(R.drawable.default_user).error(R.drawable.default_user)).into(this)
}

fun ImageView.loadCircleImageNotCache(any: Any) {
    Glide.with(this).load(any)
        .apply(RequestOptions.circleCropTransform().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).placeholder(R.drawable.default_user)
            .error(R.drawable.default_user)
        ).into(this)
}


fun ImageView.loadImageAspectRatio(url: Any?) {
    Glide.with(this).asBitmap().load(url ?: "").apply(RequestOptions().placeholder(R.drawable.default_img).error(R.drawable.default_img))
        .into(object : CustomTarget<Bitmap>() {
            override fun onLoadCleared(placeholder: Drawable?) {

            }

            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                val bitHeight = resource.height
                val bitwidth = resource.width
//                val w = PixelSizeUtil.getAndroidScreenWidth(this@loadImageAspectRatio.getContext())
                val w = this@loadImageAspectRatio.width

                var h = ((bitHeight * 1f) / bitwidth) * w

                this@loadImageAspectRatio.layoutParams = layoutParams.apply {
                    width = w.toInt()
                    height = h.toInt()
                }
                this@loadImageAspectRatio.setImageBitmap(resource)
            }

        })
}



